﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace SmartMeterParamFrame.Model
{
    /// <summary>
    /// 配置项类
    /// 在访问配置数据时：界面程序应该使用DataDoaminDP(按照可以阅读的格式储存)
    /// 而底层数据应该使用DataDomain(按照储存底层格式储存)
    /// </summary>
    public class FAinfo : DependencyObject
    {
        public static DependencyProperty PrjOrderIDProperty = DependencyProperty.Register("PrjOrderID", typeof(int), typeof(FAinfo));
        public static DependencyProperty PrjNameProperty = DependencyProperty.Register("PrjName", typeof(string), typeof(FAinfo));
        public static DependencyProperty PrjTypeProperty = DependencyProperty.Register("PrjType", typeof(EnumFAType), typeof(FAinfo));
        public static DependencyProperty BsCodeProperty = DependencyProperty.Register("BsCode", typeof(string), typeof(FAinfo));
        public static DependencyProperty DataDomainDPProperty = DependencyProperty.Register("DataDomainDP", typeof(string), typeof(FAinfo));
        public static DependencyProperty ReviewTypeProperty = DependencyProperty.Register("ReviewType", typeof(EnumCheckType), typeof(FAinfo));

        /// <summary>
        /// 指示，当前是否是在从底层设置值
        /// 如果是的话，在OnPropertyChanged事件中，不会调用更新到底层数据
        /// </summary>
        private bool isSettingValueFromLow = false;

        /// <summary>
        /// 项目排序ID
        /// </summary>
        public int PrjOrderID
        {
            get
            {
                return (int)this.Dispatcher.Invoke(new Func<int>(() => (int)base.GetValue(PrjOrderIDProperty)));
            }
            set
            {
                base.SetValue(PrjOrderIDProperty, value);
            }
        }

        /// <summary>
        /// 项目名称
        /// </summary>
        public string PrjName
        {
            get
            {
                try
                {
                    return (string)this.Dispatcher.Invoke(new Func<string>(() => (string)GetValue(PrjNameProperty)));
                }
                catch
                {
                    return (string)GetValue(PrjNameProperty);
                }
            }
            set
            {
                SetValue(PrjNameProperty, value);
            }
        }

        /// <summary>
        /// 项目类型
        /// </summary>
        public EnumFAType PrjType
        {
            get
            {
                return (EnumFAType)this.Dispatcher.Invoke(new Func<EnumFAType>(() => (EnumFAType)GetValue(PrjTypeProperty)));
            }
            set
            {
                SetValue(PrjTypeProperty, value);
            }
        }

        /// <summary>
        /// 命令标示符
        /// </summary>
        public string BsCode
        {
            get
            {
                return (string)this.Dispatcher.Invoke(new Func<string>(() => (string)GetValue(BsCodeProperty)));
            }
            set
            {
                SetValue(BsCodeProperty, value);
            }
        }

        private string dataDomain = null;

        /// <summary>
        /// 命令数据,储存的是 用于底层的数据
        /// 设置该值将自动进行转换 DataDomainDP属性
        /// </summary>
        public string DataDomain
        {
            get { return this.dataDomain; }
            set
            {
                this.dataDomain = value;
                string uiValue = value;
                // 检查di是否有效,如果有效，进行数据转换
                if (DIValueConverters.DIValueConverter.IsAllHEX(this.BsCode))
                {
                    try
                    {
                        uiValue = DIValueConverters.DIValueConverter.ConvertToUI(this.BsCode, value);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("解析底层数据失败:DI(" + this.BsCode + "),Value(" + value + "),Message(" + ex.Message + ")");
                    }
                }
                this.isSettingValueFromLow = true;
                this.DataDomainDP = uiValue;
                this.isSettingValueFromLow = false;
            }
        }

        /// <summary>
        /// 将UI数据转换成 底层数据
        /// </summary>
        /// <param name="uiValue"></param>
        /// <returns></returns>
        private bool ConverterUIDataToLowData(string uiValue)
        {
            //计算底层的值
            if (uiValue.Length == 1)
            {
                uiValue = "0" + uiValue;
            }

            string lowValue = uiValue;
            try
            {
                if (DIValueConverters.DIValueConverter.IsAllHEX(this.BsCode))
                {
                    //执行可以的转换，并更新底层数据
                    lowValue = DIValueConverters.DIValueConverter.ConvertToLow(this.BsCode, uiValue);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("解析界面数据失败:DI(" + this.BsCode + "),Value(" + uiValue + "),Message(" + ex.Message + ")");
            }
            this.dataDomain = lowValue;
            return true;
        }

        /// <summary>
        /// 命令数据，储存的是用于可以显示的 数据,使用DependencyProperty进行包装
        /// 设置该值将自动进行转换 DataDomain属性
        /// </summary>
        public string DataDomainDP
        {
            get
            {
                string value = (string)this.Dispatcher.Invoke(new Func<string>(() => (string)GetValue(DataDomainDPProperty)));
                return value;
            }
            set
            {
                this.SetValue(DataDomainDPProperty, value);
            }
        }

        /// <summary>
        /// 是否复核
        /// </summary>
        public EnumCheckType ReviewType
        {
            get { return (EnumCheckType)this.Dispatcher.Invoke(new Func<EnumCheckType>(() => (EnumCheckType)GetValue(ReviewTypeProperty))); }
            set { SetValue(ReviewTypeProperty, value); }
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            //如果是数据，且不是从底层更新数据，就将数据更新到底层中
            if (e.Property == FAinfo.DataDomainDPProperty && isSettingValueFromLow == false)
            {
                this.ConverterUIDataToLowData(e.NewValue.ToString());
            }
            base.OnPropertyChanged(e);
        }

        public override string ToString()
        {
            return PrjName;
        }
    }
}
